import * as types from '../mutation-types'

// initial state
const state = {
  devices: []
}

// actions
const actions = {
  updatParkStatus ({ commit }, deviceStatus) {
    commit(types.UPDATE_PARK_STATUS, deviceStatus)
  }
}

// getters
const getters = {
  yesterdayData () {
    let [leavePerson, leaveAuto, cash, etc, onlinePayment, prepaid, temporary, fixed, payment, into, out] = [
      0, 0, {type: 'cash', number: 0, payment: 0}, {type: 'etc', number: 0, payment: 0}, {type: 'onlinePayment', number: 0, payment: 0}, {type: 'prepaid', number: 0, payment: 0}, 0, 0, 0, 0, 0
    ]
    // 获得昨天的一整天的时间区间
    let yesterdayTime = new Date(new Date().getTime() - 86400000)
    let yYear = yesterdayTime.getFullYear()
    let yMonth = yesterdayTime.getMonth() + 1
    let yDay = yesterdayTime.getDate()
    let yesterday = new Date(`${yYear}/${yMonth}/${yDay}`)
    let nowDay = new Date(yesterday.getTime() + 86400000)
    // console.log(state.devices)
    state.devices.forEach(item => {
      let time = new Date(item.entryTime)
      let leave = new Date(item.leaveTime)
      if (yesterday < time && time < nowDay) {
        // 进场加1
        into++
        // 返回用户类型临时或固定
        if (parseInt(item.orderType) === 1) {
          temporary++
        } else if (parseInt(item.orderType) === 2) {
          fixed++
        }
      }
      if (yesterday < leave && leave < nowDay) {
        // 出场+1
        out++
        // 返回支付类型
        if (parseInt(item.payWay) === 0) {
          cash.number++
          cash.payment += item.amount
        } else if (parseInt(item.payWay) === 3) {
          etc.number++
          etc.payment += item.amount
        } else if (parseInt(item.payWay) === 4) {
          onlinePayment.number++
          onlinePayment.payment += item.amount
        } else if (parseInt(item.payWay) === 6) {
          prepaid.number++
          prepaid.payment += item.amount
        }
        payment += item.amount
        // 返回离场方式
        if (parseInt(item.leaveWay) === 1) {
          leavePerson++
        } else if (parseInt(item.leaveWay) === 0) {
          leaveAuto++
        }
      }
    })
    return [leavePerson, leaveAuto, cash, etc, onlinePayment, prepaid, temporary, fixed, payment, into, out]
  },
  // 停车场今天(支付数值，入闸数，出闸数)
  nowDay () {
    let [payment, into, out] = [0, 0, 0]
    // 获得今天0点到当前时间
    let Year = new Date().getFullYear()
    let Month = new Date().getMonth() + 1
    let Day = new Date().getDate()
    let nowday = new Date(`${Year}/${Month}/${Day}`)
    let nowTime = new Date()
    state.devices.forEach(item => {
      let time = new Date(item.entryTime)
      if (nowday < time && time < nowTime) {
        into++
      }
      let leave = new Date(item.leaveTime)
      if (nowday < leave && leave < nowTime) {
        out++
        payment += item.amount
      }
    })
    return [payment, into, out]
  },
  // 最近24个小时(消费数值，入闸数，出闸数)
  lastTwentyFour () {
    // 获得最近24小时的区间
    let yesterdayTime = new Date(new Date().getTime() - 86400000)
    let nowTime = new Date()
    let [payment, intoHourNumber, outHourNumber] = [[], [], []]
    // 给予初始0值
    for (let i = 0; i < 24; i++) {
      payment.push(0)
      intoHourNumber.push(0)
      outHourNumber.push(0)
    }
    state.devices.forEach(item => {
      let time = new Date(item.entryTime)
      let leave = new Date(item.leaveTime)
      let intoHours = time.getHours()
      let intoMinutes = time.getMinutes()
      let leaveHours = leave.getHours()
      let leaveMinutes = leave.getMinutes()
      let nowHours = new Date().getHours()
      let nowMinutes = new Date().getMinutes()
      // 判断入闸时间是否属于最近24小时
      if (yesterdayTime < time && time < nowTime) {
        let difference = intoHours - nowHours
        if (difference > 0) {
          intoHourNumber[difference]++
        } else if (difference < 0) {
          intoHourNumber[difference + 23]++
        } else if (difference === 0) {
          // 当是临界点时需要加上分钟对比
          if (nowMinutes > intoMinutes) {
            intoHourNumber[difference + 23]++
          } else {
            intoHourNumber[difference]++
          }
        }
      }
      // 判断出闸时间是否属于24小时
      if (yesterdayTime < leave && leave < nowTime) {
        let difference = leaveHours - nowHours
        if (difference > 0) {
          outHourNumber[difference]++
          // 返回来的数值单位是分
          payment[difference] += item.amount / 100
        } else if (difference < 0) {
          outHourNumber[difference + 23]++
          payment[difference + 23] += item.amount / 100
        } else if (difference === 0) {
          if (nowMinutes > leaveMinutes) {
            outHourNumber[difference + 23]++
            payment[difference + 23] += item.amount / 100
          } else {
            outHourNumber[difference]++
            payment[difference] += item.amount / 100
          }
        }
      }
    })
    // return [[200, 380, 320, 380, 280, 420, 400, 340, 200, 380, 320, 380, 280, 420, 400, 430, 200, 380, 320, 380, 280, 120, 100, 110], [200, 380, 320, 380, 280, 120, 100, 110, 200, 380, 320, 380, 280, 120, 100, 110, 200, 380, 320, 380, 280, 120, 100, 110], [230, 310, 320, 310, 210, 160, 130, 110, 230, 310, 360, 310, 210, 160, 130, 110, 230, 310, 360, 310, 210, 160, 130, 110]]
    return [payment, intoHourNumber, outHourNumber]
  }
}

// mutations
const mutations = {
  [types.UPDATE_PARK_STATUS] (state, deviceStatus) {
    state.devices = deviceStatus
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
